package com.wtzz.police.base.config;

import com.wtzz.police.base.entity.User;
import com.wtzz.police.base.util.WebTool;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.Optional;

/**
 * 自动将当前用户id赋值给 修改AbstractAuditingEntity对象的createdBy与modifiedBy
 */
@Configuration
@EnableJpaAuditing
public class JpaAuditorAware implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        User u = WebTool.getCurrentUser();
        return u!=null?Optional.of(u.getId()):Optional.empty();
    }
}
